package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.BusinessMapper;
import com.qf.entitys.Business;
import com.qf.entitys.BusinessWithBLOBs;
import com.qf.service.BusinessService;
import com.qf.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessMapper businessMapper;

    @Resource
    private BusinessMapper mapper;

    @Override
    public int deleteByPrimaryKey(Integer bid) {
        return mapper.deleteByPrimaryKey(bid);
    }

    @Override
    public int insert(BusinessWithBLOBs record) {
        return 0;
    }

    @Override
    public int insertSelective(Business record) {
        record.setBloginPwd(MD5Utils.getMD5(record.getBloginPwd()));
        return businessMapper.insertSelective(record);
    }

    @Override
    public BusinessWithBLOBs selectByPrimaryKey(Integer bid) {
        return mapper.selectByPrimaryKey(bid);
    }

    @Override
    public int updateByPrimaryKeySelective(BusinessWithBLOBs record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(BusinessWithBLOBs record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Business record) {
        return 0;
    }

    @Override
    public PageInfo<BusinessWithBLOBs> selectAll(Integer pageindex,Business record) {
        PageHelper.startPage(pageindex,10);
        List<BusinessWithBLOBs> list = mapper.selectAll(record);
        //封装分页里的list数据属性
        PageInfo<BusinessWithBLOBs> businessPageInfo = new PageInfo<>(list);
        return businessPageInfo;
    }

    @Override
    public int updateBactive(Integer bid, Integer bactive) {
        return mapper.updateBactive(bid, bactive);
    }

    @Override
    public BusinessWithBLOBs selectDetailByid(Integer bid) {
        return mapper.selectDetailByid(bid);
    }

    /**
     * 批量通过或冻结账户
     * @param ids
     * @return
     */
    @Override
    public int updateBactiveBySel(Integer[] ids,Integer bactive) {
        for (Integer id : ids) {
            int i = updateBactive(id, bactive);
            if (i==0){
                throw new RuntimeException("修改失败");
            }
        }
        return 1;
    }

    /**
     * 批量拒绝申请
     * @param ids
     * @return
     */
    @Override
    public int delbyids(Integer[] ids) {
        for (Integer id : ids) {
            int i = deleteByPrimaryKey(id);
            if (i==0){
                throw new RuntimeException("操作失败");
            }
        }
        return 1;
    }

    @Override
    public Business selectByIdAndPwd(Business record) {
        //将获取到的密码通过spring自带的加密工具进行加密
        record.setBloginPwd(DigestUtils.md5DigestAsHex(record.getBloginPwd().getBytes()));
        return mapper.selectByIdAndPwd(record);
    }

    @Override
    public List<BusinessWithBLOBs> selectAllByMillis(double log, double lat) {
//        PageHelper.startPage(pageindex,5);
        List<BusinessWithBLOBs> businessWithBLOBs = mapper.selectAllByMillis(log, lat);
//        PageInfo<BusinessWithBLOBs> businessWithBLOBsPageInfo = new PageInfo<>(businessWithBLOBs);
        return businessWithBLOBs;
    }
}
